-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Clean up DB sessions consistently #702
Closed
Closed
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AndreasAlbertQC
added
bug
Something isn't working
dependencies
Pull requests that update a dependency file
maintenance
labels
Jul 15, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Context / original problem
In our deployment, we have recently started seeing quetz crashes with the following error message:
sqlalchemy.exc.TimeoutError: QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30.00 (Background on this error at: https://sqlalche.me/e/20/3o7r)
This struck me as odd because we configure the pool / overflow sizes manually via the quetz settings / environment variables. The values in the error message neither match our settings nor the quetz default settings, indicating that both sets of settings are ignored.
Diagnosis
I have traced this to the fact that our main entrypoints for connecting to the database (get_engine and get_session) are used inconsistently in the code base. Effectively, it is currently up to the caller to make sure that the right settings are propagated from the configuration to the kwargs for these functions, which often does not happen. Therefore, some connections are configured correctly, some are not.
While looking through the code, I also noticed that we also treat session management inconsistently throughout the code base. Sometimes we protect sessions via a contextmanager that closes the session after use, sometimes we do it manually, sometimes we never close the session.
Proposed solution / changes
Session
objects offer the context manager protocol and automatically handle cleanup.get_session(config: Config | None) -> Session
. By default, ifconfig
is not provided,get_session
will use the default globalConfig
object. Sessions obtained in this way are guaranteed to respect the config settings. It is now easy to grok if sessions are handled correctly: If you seewith get_session(...) as db:
, everything is good. If you seeget_session
used in any other way, it's bad.